package com.hsy.bibc.modules.msg.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.deparse.dpframe.utils.DPPicassoUtil;
import com.hsy.bibc.common.Consts;
import com.hsy.bibc.modules.common.CommonBaseFragment;
import com.hsy.bibc.modules.common.PullableListView;
import com.hsy.bibc.modules.msg.activity.ActivityMsgDetail;
import com.hsy.bibc.modules.msg.gsondata.MsgListData;
import com.hsy.bibc.R;
import com.hsy.bibc.utils.CommonUtils;
import com.hsy.bibc.utils.HttpUtil;
import com.jingchen.pulltorefresh.PullToRefreshLayout;

import java.util.List;

/**
 * Created by hsy on 2016/4/7.
 */
public class FragmentMsg extends CommonBaseFragment {
    private static final int TYPE_GET_LIST = 0;
    private static final int TYPE_REFRESH_LIST = 1;
    private static final int TYPE_LOAD_LIST = 2;

    private MsgAdapter adapterFragmentInformation;
    private PullableListView list_view;
    private List<MsgListData.Data> listData;
    private LayoutInflater inflater;

    private String nextPage = null;

    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            DPLogUtil.d("response=" + response);
            if (TYPE_GET_LIST == what) {

                MsgListData.Result result = DPGsonUtil.fromJson(response.trim(), MsgListData.Result.class);
                showList(result);

                CommonUtils.hideProgressDialog();
            } else if (TYPE_REFRESH_LIST == what) {
                listData.clear();
                MsgListData.Result result = DPGsonUtil.fromJson(response.trim(), MsgListData.Result.class);
                showList(result);
                list_view.refreshFinish(PullableListView.REFRESH_FINISH);
                getRootView().setVisibility(View.VISIBLE);
            } else if (TYPE_LOAD_LIST == what) {
                MsgListData.Result result = DPGsonUtil.fromJson(response.trim(), MsgListData.Result.class);
                nextPage = result.getNextPage();
                listData.addAll(result.getData());
                adapterFragmentInformation.notifyDataSetChanged();
                getRootView().setVisibility(View.VISIBLE);
                list_view.loadmoreFinish(PullableListView.LOAD_FINISH);
            }
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            if (TYPE_GET_LIST == what) {
                CommonUtils.hideProgressDialog();
            } else if (TYPE_REFRESH_LIST == what) {
                list_view.refreshFinish(PullableListView.FAIL);
            } else if (TYPE_LOAD_LIST == what) {
                list_view.loadmoreFinish(PullableListView.FAIL);
            }
        }

        @Override
        public void onMsgError(int what, int type, String msg) {
            if (TYPE_GET_LIST == what) {
                CommonUtils.hideProgressDialog();
            } else if (TYPE_REFRESH_LIST == what) {
                list_view.refreshFinish(PullableListView.FAIL);
            } else if (TYPE_LOAD_LIST == what) {
                list_view.loadmoreFinish(PullableListView.FAIL);
            }
        }
    };

    private void showList(MsgListData.Result result) {
        nextPage = result.getNextPage();

        listData = result.getData();
        adapterFragmentInformation = new MsgAdapter();
        list_view.setAdapter(adapterFragmentInformation);
        getRootView().setVisibility(View.VISIBLE);
    }

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_msg;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        getRootView().setVisibility(View.GONE);

        inflater = LayoutInflater.from(getAttchedActivity());
        list_view = F(R.id.list);


        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getAttchedActivity(), ActivityMsgDetail.class);
                intent.putExtra("id", listData.get(position).getId());
                DPLogUtil.d("id==" + id);
                startActivity(intent);
            }
        });


        list_view.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                HttpUtil.httpGet(getAttchedActivity(), TYPE_REFRESH_LIST, Consts.API_GET_NEWS_ZX_LIST, callback);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

                if ("0".equals(nextPage)) {
                    showToast(R.string.txt_no_more_data);
                    list_view.loadmoreFinish(PullableListView.LOAD_FINISH);
                    return;
                }
                HttpUtil.httpGet(getAttchedActivity(), TYPE_LOAD_LIST, Consts.API_GET_NEWS_ZX_LIST + "?page=" + nextPage, callback);
            }
        });
        CommonUtils.showProgressDialog(getAttchedActivity());
        HttpUtil.httpGet(getAttchedActivity(), TYPE_GET_LIST, Consts.API_GET_NEWS_ZX_LIST, callback);
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void onHandleMessage(Message msg) {

    }

    @Override
    public void onFragmentUpdate() {

    }

    private class MsgAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int i) {
            return listData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            view = inflater.inflate(R.layout.item_news, null);

            TextView date_time = (TextView) view.findViewById(R.id.date_time);
            TextView title_name = (TextView) view.findViewById(R.id.title_name);
            TextView view_num = (TextView) view.findViewById(R.id.view_num);
            ImageView list_image = (ImageView) view.findViewById(R.id.list_image);

            date_time.setText(listData.get(position).getCreate_datetime_str());
            title_name.setText(listData.get(position).getName());

            if (null == listData.get(position).getImg() || "".equals(listData.get(position).getImg())) {
                list_image.setVisibility(View.GONE);
            } else {
                list_image.setVisibility(View.VISIBLE);
                DPPicassoUtil.with(getAttchedActivity()).load(listData.get(position).getImg()).into(list_image);
            }


            return view;
        }
    }

}
